#!/bin/bash

# 检测ruby是否安装
if [ ! `which ruby` ]; then
    sudo apt-get install -y ruby
    gem sources -r https://rubygems.org
    gem sources -a https://gems.ruby-china.org
fi

# 检测puppet是否安装
if [ ! `which puppet` ]; then
    sudo gem install puppet
fi

MODULEPATH='./modules'

# 安装puppet module
install_module() {
    module_name=$1
    module_dir=${module_name#*-}
    if [ ! -d "${MODULEPATH}/${module_dir}" ]; then
        puppet module install $module_name --modulepath=$MODULEPATH
    else
        echo "$module_name already installed, skip."
    fi
}

install_module "puppetlabs-apt"

sudo puppet apply --hiera_config=./hiera.yaml --modulepath=$MODULEPATH ./manifests/${HOSTNAME}.pp
